<?php
namespace app\admin\controller;
use think\Controller;
use think\Session;
use think\Cookie;
use think\Jump;
use think\Db;
class Index extends Controller{
    public function index()
    {

      if(!empty(Session::get('user'))){
        $user = Session::get('user');
        $this->assign("user",$user);
        return $this->fetch('admin/index');
      
      }else{
       return $this->fetch("admin/login");
      }
    }
    public function login(){
    	if(request()->isget()){
    			
    	return $this->fetch("admin/login");
    	}
    	if (request()->ispost()) {
         $username = $_REQUEST['username'];
    	   $pwd = $_REQUEST['password'];
         $sql =  "select salt from ims_users where username = '{$username}'";
         $salt = DB::query($sql)[0]['salt'];
         if ($salt) {
           $password = md5($pwd.$salt);
            $sql =  "select user_id from ims_users where username = '{$username}' and password = '{$password}'";
            $uid = DB::query($sql)[0];
            if($uid){
              $sql = "select * from ims_mc_members as m left join ims_xuan_zy_person as p on m.uid=p.uid where m.uid = {$uid['user_id']}";
              $user = DB::query($sql)[0];
              if(empty($user)){
                $user['nickname'] = $username;
                $user['name'] = $username;
                $user['power'] = 0;
              }
              $user['salt'] = $salt;

              Session::set("user",$user);   

              return $this->success("登录成功","admin/index/index");
           
            } else {
          $this->error("密码不正确");
            }
         } else {
          $this->error("用户名不正确");
         }
    	}
    }
    
}
